﻿// -----------------------------------------------------------------------
// <copyright file="CauHinhChiTietDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class CauHinhChiTietDataAccess
    {
        public Int64 InsertCauHinhChiTiet(CauHinhChiTietEntity obj)
        {
            try
            {
                string sql = "spCauHinhChiTiet_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddOutParameter(cmd, "MaCauHinhChiTiet", DbType.Int64, int.MaxValue);
                db.AddInParameter(cmd, "MaCauHinh", DbType.Int64, obj.MaCauHinh);
                db.AddInParameter(cmd, "MaPhongKham", DbType.String, obj.MaPhongKham);
                db.ExecuteNonQuery(cmd);
                return (Int64)db.GetParameterValue(cmd, "MaCauHinhChiTiet");
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertCauHinhChiTiet", ex.Message); throw ex; }
        }

        public bool EditCauHinhChiTiet(CauHinhChiTietEntity obj)
        {
            try
            {
                string sql = "spCauHinhChiTiet_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaCauHinhChiTiet", DbType.Int64, obj.MaCauHinhChiTiet);
                db.AddInParameter(cmd, "MaCauHinh", DbType.Int64, obj.MaCauHinh);
                db.AddInParameter(cmd, "MaPhongKham", DbType.String, obj.MaPhongKham);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateCauHinhChiTiet", ex.Message); throw ex; }
        }

        public bool DeleteCauHinhChiTiet(Int64 maCauHinh)
        {
            try
            {
                string sql = "spCauHinhChiTiet_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaCauHinh", DbType.Int64, maCauHinh);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteCauHinhChiTiet", ex.Message); throw ex; }
        }

        public CauHinhChiTietEntity GetCauHinhChiTiet(Int64 maCauHinhChiTiet)
        {
            try
            {
                CauHinhChiTietEntity obj = null;
                string sql = "spCauHinhChiTiet_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaCauHinhChiTiet", DbType.Int64, maCauHinhChiTiet);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new CauHinhChiTietEntity();
                        obj.MaCauHinhChiTiet = String.IsNullOrEmpty(reader["MaCauHinhChiTiet"].ToString()) == true ? 0 : (Int64)reader["MaCauHinhChiTiet"];
                        obj.MaCauHinh = String.IsNullOrEmpty(reader["MaCauHinh"].ToString()) == true ? 0 : (Int64)reader["MaCauHinh"];
                        obj.MaPhongKham = reader["MaPhongKham"].ToString();
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetCauHinhChiTiet", ex.Message); throw ex; }
        }

        public List<CauHinhChiTietEntity> GetCauHinhChiTietList(Int64 maCauHinh)
        {
            try
            {
                List<CauHinhChiTietEntity> list = new List<CauHinhChiTietEntity>();
                CauHinhChiTietEntity obj = null;
                string sql = "spCauHinhChiTiet_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaCauHinh", DbType.String, maCauHinh);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new CauHinhChiTietEntity();
                        obj.MaCauHinhChiTiet = String.IsNullOrEmpty(reader["MaCauHinhChiTiet"].ToString()) == true ? 0 : (Int64)reader["MaCauHinhChiTiet"];
                        obj.MaCauHinh = String.IsNullOrEmpty(reader["MaCauHinh"].ToString()) == true ? 0 : (Int64)reader["MaCauHinh"];
                        obj.MaPhongKham = reader["MaPhongKham"].ToString();
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetCauHinhChiTietList", ex.Message); throw ex; }
        }

    }
}